﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Autorent.Desktop.Modules.Orders;
using Autorent.Desktop.Modules.Autoes;
using Autorent.Desktop.Modules.Clients;
using Autorent.Desktop.Modules.Employees;
using Autorent.Desktop.Modules.Offices;
using Autorent.Desktop.Modules.Reports;
using Autorent.Desktop.Modules.Addresses;
using Autorent.Desktop.Views;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.UnityExtensions;
using Autorent.Desktop.Model.Services;

namespace Autorent.Desktop
{
    public class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            //Create and show Shell
            Shell shell = Container.Resolve<Shell>();
            shell.DataContext = Container.Resolve<ShellViewModel>();
            shell.Show();

            //Handle all unhandled exceptions of app
            Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
            return shell;
        }

        protected override void ConfigureModuleCatalog()
        {
            ModuleCatalog.AddModule(
                new ModuleInfo()
                {
                    ModuleName = typeof(OrdersModule).Name,
                    ModuleType = typeof(OrdersModule).AssemblyQualifiedName
                });
            ModuleCatalog.AddModule(
                new ModuleInfo()
                {
                    ModuleName = typeof(AutoesModule).Name,
                    ModuleType = typeof(AutoesModule).AssemblyQualifiedName
                });
            ModuleCatalog.AddModule(
               new ModuleInfo()
               {
                   ModuleName = typeof(EmployeesModule).Name,
                   ModuleType = typeof(EmployeesModule).AssemblyQualifiedName
               });
            ModuleCatalog.AddModule(
               new ModuleInfo()
               {
                   ModuleName = typeof(ClientsModule).Name,
                   ModuleType = typeof(ClientsModule).AssemblyQualifiedName
               });
            ModuleCatalog.AddModule(
               new ModuleInfo()
               {
                   ModuleName = typeof(OfficesModule).Name,
                   ModuleType = typeof(OfficesModule).AssemblyQualifiedName
               });
            ModuleCatalog.AddModule(
               new ModuleInfo()
               {
                   ModuleName = typeof(ReportsModule).Name,
                   ModuleType = typeof(ReportsModule).AssemblyQualifiedName
               });
            ModuleCatalog.AddModule(
               new ModuleInfo()
               {
                   ModuleName = typeof(AddressesModule).Name,
                   ModuleType = typeof(AddressesModule).AssemblyQualifiedName
               });
        }

        //Exception handler
        private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message);
            e.Handled = true;
        }
    }
}
